package rsj.service.report;

import com.alibaba.fastjson.JSON;
import com.rlyy.aibase.utils.base.TableNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rsj.api.report.entity.CustomerReport;
import rsj.api.report.entity.CustomerReportDetail;
import rsj.api.report.entity.ReportParam;
import rsj.sql.customer.CustomerReportDao;

import java.util.List;

/**
 * 客户报表
 *
 * @author yuanqinglong
 * @since 2021/7/22 9:12
 */
@Service
public class CustomerReportServiceImpl implements IReportService<CustomerReport, CustomerReportDetail, ReportParam> {



    @Autowired
    private CustomerReportDao customerReportDao;



    /**
     * 报告列表
     *
     * @param reportParam 报告参数
     * @return {@link List<CustomerReport>}
     */
    @Override
    public List<CustomerReport> reportList(ReportParam reportParam) {
        TableNode tableNode = customerReportDao.customerReportList(reportParam);
        List<CustomerReport> customerReports = JSON.parseArray(tableNode.toJson(), CustomerReport.class);
        customerReports.forEach(customerReport -> customerReport.processingAttributeValue(reportParam));
        return customerReports;
    }

    /**
     * 报告数
     *
     * @param reportParam 报告参数
     * @return int
     */
    @Override
    public int reportCount(ReportParam reportParam) {
        return customerReportDao.customerReportCount(reportParam).valueInt();
    }

    /**
     * 详细的列表
     *
     * @param reportParam 报告参数
     * @return {@link List<CustomerReportDetail>}
     */
    @Override
    public List<CustomerReportDetail> detailList(ReportParam reportParam) {
        TableNode detailList = customerReportDao.customerReportDetailList(reportParam);
        List<CustomerReportDetail> customerReportDetailList = JSON.parseArray(detailList.toJson(), CustomerReportDetail.class);
        customerReportDetailList.forEach(CustomerReportDetail::calculateRate);
        return customerReportDetailList;
    }

    /**
     * 详细的计算
     *
     * @param reportParam 报告参数
     * @return int
     */
    @Override
    public int detailCount(ReportParam reportParam) {
        return customerReportDao.customerReportDetailCount(reportParam).valueInt();
    }


}
